package test.baojia.act.operation;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.testng.Assert;

import java.util.Properties;

/**
 * 写入全局变量操作。
 */
public class WriteGlobalOperation extends WriteOperation {

    public void run(JSONObject context, WebDriver driver, Properties resource, JSONObject parentContext) throws Exception{
        super.run(context , driver , resource, parentContext);

        String globalValue = null;

        final String byValueType = context.getString(OPERATION.byValueType.toString());
        if(OPERATION.byValue.toString().equals(byValueType)){
            globalValue = this.makeWebElement(false).getAttribute("value");
        }else if(OPERATION.byText.toString().equals(byValueType)){
            globalValue = this.makeWebElement(false).getText();
        }else {
            Assert.assertTrue(false , String.format("The %s has no match." , byValueType));
        }

        final String globalKey = context.getString(GLOBAL);
        Assert.assertTrue(StringUtils.isNotBlank(globalKey));
        this.putGlobalProperty(globalKey , globalValue);
    }

}
